#pragma once
#include <functional>
#include <memory>
#include <sys/poll.h>
#include <unordered_map>
#include <vector>
#include "timer_event.h"
class channel;
class wakeuper;

class poll_poller
{
  public:
    poll_poller();
    ~poll_poller();
    void start();

  public:
    void add_channel(std::shared_ptr<channel> conn);
    void remove_channel(std::shared_ptr<channel> conn);
    void update_channel(std::shared_ptr<channel> conn);
    void loop();
    void run_in_loop(std::function<void()> func);

    void add_timer_event(const timer_event& event);

  private:
    void handle_timer_event();

  private:
    using PollFdList = std::vector<struct pollfd>;
    PollFdList                                        pollfds_;
    std::unordered_map<int, std::shared_ptr<channel>> channels_;
    std::shared_ptr<wakeuper>                         wakeuper_;
    std::unordered_map<int, timer_event>              timer_events_;
};